package com.koocloud.electroplatemanage.service.impl;

import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.CurrentLocalMapper;
import com.koocloud.electroplatemanage.pojo.mainpojo.CurrentLocationCount;
import com.koocloud.electroplatemanage.service.CurrentLocalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;


/**
 * @program:
 * @description:
 * @author: wjx
 * @create: 2020-11-23 10:21
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CurrentLocalServiceImpl implements CurrentLocalService {

    @Resource
    CurrentLocalMapper currentLocalMapper;

    @Override
    public ResponseTemplate selectAllData() {
        try {

            List<CurrentLocationCount> list =  currentLocalMapper.selectAllData();
            if (!list.isEmpty()){
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(list).build();
            } else {
                //没有查询到数据
                return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(1l).data(list).build();
            }
        }catch (Exception e){
            //事务手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //遇到异常，返回异常原因
            e.printStackTrace();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
}
